package cn.edu.answer.controller;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.answer.po.Classes;
import cn.edu.answer.service.ClassService;
import cn.edu.answer.utils.Page;



/**
 * 班级管理控制器类
 */
@Controller
public class ClassController {
	// 依赖注入
	@Autowired
	private ClassService classService;


	/**
	 *  班级列表
	 */
	@RequestMapping(value = "/class/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows, Model model) {
		// 所有学生列表
		Page<Classes> classes = classService.findClassList(page,rows);
		model.addAttribute("page", classes);	
		System.out.println("===="+classes.getSize());
		System.out.println("===="+classes);
		return "class";
	}
	
	/**
	 * 添加班级
	 */
	@RequestMapping("/class/createClass.action")
	@ResponseBody
	public String cateClass(Classes classes,HttpSession session) {

	    // 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = classService.createclass(classes);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	/**
	 * 删除班级信息
	 */
	@RequestMapping("/class/deleteClass.action")
	@ResponseBody
	public String classDelete(String classid) {
	    int rows = classService.deleteClass(classid);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	
	/**
	 * 通过id获取班级信息
	 */
	@RequestMapping("/class/getClassById.action")
	@ResponseBody
	public Classes getClassById(String classid) {
		 System.out.println("====="+classid);
	    
	    Classes classes=classService.getClassById(classid);
	    System.out.println("====="+classid);
	    return classes;
	}
	/**
	 * 更新班级
	 */
	@RequestMapping("/class/updateClass.action")
	@ResponseBody
	public String classUpdate(Classes classes) {
		   System.out.println("======="+classes);
	    int rows = classService.updateClass(classes);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}

}
